export function useRefModel<T>(
  props: T,
  propName: Exclude<keyof T, Symbol>,
  emit: Function
): any {
  return computed({
    get: () => props[propName],
    set: (value) => emit(`update:${propName}`, value),
  });
}

let cacheMap = new WeakMap();
export function useVModel(
  props: { [x: string]: object },
  propName: string | number,
  emit: (arg0: string, arg1: any) => void
) {
  return computed({
    get() {
      if (cacheMap.has(props[propName])) return cacheMap.get(props[propName]);
      let proxy = new Proxy(props[propName], {
        get: (target, key) => Reflect.get(target, key),
        set(target, key, value) {
          emit(`update:${propName}`, {
            ...target,
            [key]: value,
          });
          return true;
        },
      });
      cacheMap.set(props[propName], proxy);
      return proxy;
    },
    set: (value) => emit(`update:${propName}`, value),
  });
}

export function useReactiveModel<T>(
  props: T,
  propName: Exclude<keyof T, Symbol>,
  emit: Function
): any {
  return computed({
    get: () =>
      new Proxy(props[propName] as Object, {
        get: (target, key) => Reflect.get(target, key),
        set(target, key, value) {
          emit(`update:${propName}`, {
            ...target,
            [key]: value,
          });
          return true;
        },
      }),
    set: (value) => emit(`update:${propName}`, value),
  });
}
